import QtQuick 2.12

Rectangle {
    color: "#000"
    property int pan: 16
    property int movThreshold: 8
    property int minimumWidth: 512
    property int minimumHeight: 160+20
    property int mouseAxisZ: 1
    MouseArea {
        //id: top
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeAllCursor;

        }

        onPressed: {
            oldX = mouseX;
            oldY = mouseY;

        }
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy) > parent.movThreshold) {
                    parent.x = parent.x + dx
                    parent.y = parent.y + dy
                }

            }

        }

        height: parent.pan
        width: parent.width-2*parent.pan
        y: 0
        x: (parent.width-width)/2

    }
    MouseArea {
        //id: topLeft
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeFDiagCursor

        }

        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    var newWidth = Math.max(parent.width-dx, parent.minimumWidth)
                    var newHeight = Math.max(parent.height-dy, parent.minimumHeight)
                    parent.x = parent.x + (parent.width-newWidth )
                    parent.y = parent.y + (parent.height-newHeight)
                    parent.width = newWidth
                    parent.height = newHeight

                }


            }

        }
        y: 0
        x: 0
        height: parent.pan
        width: parent.pan

    }
    MouseArea {
        //id: bottomLeft
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeBDiagCursor
        }

        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    var newWidth = Math.max(parent.width-dx, parent.minimumWidth)
                    parent.x = parent.x + (parent.width-newWidth )
                    parent.width = newWidth
                    parent.height =  Math.max(parent.height+dy, parent.minimumHeight)

                }
            }
        }
        height: parent.pan
        width: parent.pan
        y: parent.height-height
        x: 0


    }
    MouseArea {
        //id: topRight
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeBDiagCursor
        }

        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    parent.width = Math.max(parent.width+dx, parent.minimumWidth)
                    var newHeight = Math.max(parent.height-dy, parent.minimumHeight)
                    parent.y = parent.y + (parent.height-newHeight )
                    parent.height = newHeight

                }
            }
        }
        height: parent.pan
        width: parent.pan
        y: 0
        x: parent.width-width

    }
    MouseArea {
        //id: left
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeHorCursor
        }

        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    var newWidth = Math.max(parent.width - dx, parent.minimumWidth)
                    parent.x = parent.x + (parent.width-newWidth)
                    parent.width = newWidth
                }

            }

        }
        y: parent.pan
        x: 0
        height: parent.height - parent.pan*2
        width: parent.pan

    }
    MouseArea {
        //id: right
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeHorCursor
        }

        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    parent.width = Math.max(parent.width + dx, parent.minimumWidth)

                }

            }

        }
        height: parent.height - parent.pan*2
        width: parent.pan
        y: parent.pan
        x: parent.width-width


    }
    MouseArea {
        id: bottom
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeVerCursor

        }

        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    parent.height = Math.max(parent.height + dy, parent.minimumHeight)
                }


            }

        }
        y: parent.height-height
        x: parent.pan
        height: parent.pan
        width: parent.width-2*parent.pan

    }
    MouseArea {
        id: bottomRight
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        z: parent.mouseAxisZ
        property real oldX
        property real oldY

        onEntered: {
            cursorShape = Qt.SizeFDiagCursor

        }

        onPressed: {
            oldX = mouseX;
            oldY = mouseY;

        }
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>parent.movThreshold) {
                    var newWidth = Math.max(parent.width+dx, parent.minimumWidth)
                    var newHeight = Math.max(parent.height+dy, parent.minimumHeight)
                    parent.width = newWidth
                    parent.height = newHeight
                }

            }

        }
        height: parent.pan
        width: parent.pan
        y: parent.height-height
        x: parent.width-width
    }
}
